﻿function ECommerceProductDetailsViewModel(context) {

    var self = this;

    var spinner = new SpinnerModel();

    var dispatcher = $({});

    var productsByTag = context.spaRuntime.findRouteByName('productsByTag');

    var product = ko.observable(null);
    var isNew = false;

    var newTag = ko.observable();
    
    var viewUrl = ko.computed(function () {
        var pr = product();
        if (!pr) return null;
        return context.spaRuntime.findRouteByName('product').formatHash({
            id: pr.id()
        });
    });

    function submit() {
        var pr = product();
        var info = pr.getInfo();
        var api = context.api();
        if (isNew) {
            spinner.enter();
            api.products.createProduct(info, function() {
                spinner.leave();
                window.location.hash = viewUrl();
                dispatcher.trigger('success');
            }, function() {
                spinner.leave();
            });
        } else {
            spinner.enter();
            api.products.updateProduct(pr.id(), info, function () {
                spinner.leave();
                window.location.hash = viewUrl();
                dispatcher.trigger('success');
            }, function () {
                spinner.leave();
            });
        }
    }
    
    function removeProduct() {
        var pr = product();
        if (!isNew) {
            var api = context.api();

            spinner.enter();
            api.products.removeProduct(pr.id(), function () {
                spinner.leave();
                window.location.hash = context.spaRuntime.findRouteByName('products').formatHash({});
                dispatcher.trigger('success');
            }, function () {
                spinner.leave();
            });
        }
    }

    function showFor(prodId) {
        isNew = false;
        var api = context.api();

        spinner.enter();
        api.products.getDetailsById(prodId, function (response) {
            spinner.leave();

            var pr = new FullProductModel(response);
            product(pr);
        }, function () {
            spinner.leave();
        });

    }

    function showForNew() {
        isNew = true;
        var pr = new FullProductModel();
        product(pr);
    }

    function getByTagUrl(tag) {
        return productsByTag.formatHash({
            tag: tag
        });
    }
    
    function addNewTag() {
        var tag = newTag();
        if (tag) {
            product().tags.push(tag);
            newTag('');
        }
    }
    
    function addOnSuccess(handler) {
        dispatcher.bind('success', handler);
    }

    self.product = product;
    self.submit = submit;
    self.removeProduct = removeProduct;
    self.showFor = showFor;
    self.showForNew = showForNew;
    self.getByTagUrl = getByTagUrl;
    self.viewUrl = viewUrl;
    self.newTag = newTag;
    self.addNewTag = addNewTag;
    self.addOnSuccess = addOnSuccess;
}

